package utils

import (
	"fmt"
	"strconv"
	"strings"
)

// ArrInt64ToStr 将 [1,2,3] 转换为字符串: "1,2,3"
func ArrInt64ToStr(in []int64) (str string) {
	if len(in) == 0 {
		return
	}
	for _, item := range in {
		str += fmt.Sprintf("%v,", item)
	}
	return str[:len(str)-1]
}

// ArrStringToStr 将 ["a","b","c"] 转换为字符串 "a,b,c"
func ArrStringToStr(in []string) (str string) {
	if len(in) == 0 {
		return
	}
	for _, item := range in {
		str += fmt.Sprintf("%v,", item)
	}
	return str[:len(str)-1]
}

func StringToArr(str string) (arr []string) {
	arr = strings.Split(str, ",")
	return
}

func StrToInt(str string) (num int) {
	num, err := strconv.Atoi(str)
	if err != nil {
		num = 0
	}
	return num
}

func StrToInt32(str string) (num int64) {
	num, err := strconv.ParseInt(str, 10, 32)
	if err != nil {
		num = 0
	}
	return num
}
func StrToInt64(str string) (num int64) {
	num, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		num = 0
	}
	return num
}

func StrToFloat(str string) (num float64) {
	num, _ = strconv.ParseFloat(str, 64)
	return num
}

func StrToBool(str string) (b bool) {
	valstr := strings.ToLower(str)
	switch valstr {
	case "true":
		return true
	case "1":
		return true
	}
	return false
}

// GetBetweenContent 获取中间内容
func GetBetweenContent(str, start, end string) string {
	startIndex := strings.Index(str, start)
	endIndex := strings.Index(str, end)
	if startIndex == -1 || endIndex == -1 || startIndex >= endIndex {
		return ""
	}
	return str[startIndex+1 : endIndex]
}
